package com.zhu.tool_test.starke.matrix.multipart.core;


import com.zhu.tool_test.starke.matrix.multipart.annotations.Camel;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Qicheng Peng
 * @version V1.0
 * @description
 * @date 2019-03-02
 */
@Aspect
@Component
@Slf4j
public class CamelAspect {


    /**
     * 数据返回之前直接转化为驼峰
     *
     * @param joinPoint 切入点
     * @param camel     注解
     * @return 数据
     */
    @Around(value = "@annotation(camel)")
    public Object convertWhenReturn(ProceedingJoinPoint joinPoint, Camel camel) {
        Object data = null;
        try {
            data = joinPoint.proceed();
            if (null != data) {
                if (data instanceof MultipartData) {
                    ((MultipartData) data).translate2Camel();
                }
                if (data instanceof List) {
                    if (((List) data).size() > 0 && ((List) data).get(0) instanceof MultipartData) {
                        @SuppressWarnings("unchecked")
                        List<MultipartData> tmp = (List<MultipartData>) data;
                        List<MultipartData> target = new ArrayList<>();
                        for (MultipartData multipartData : tmp) {
                            target.add(multipartData.translate2Camel());
                        }
                        return target;
                    }
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return data;
    }

}
